package ec.fugu.amf;

//import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
//import javax.microedition.io.OutputConnection;

import java.io.IOException;
import java.util.Hashtable;
import java.util.Vector;

public class AmfClient {
	private String endPoint;
	private DataOutputStream outputStream;
	private DataInputStream inputStream;
	private AmfMessage request;
	private AmfMessage response;
	private int encoding = AmfConstants.AMF0;
	private long elapsedTime = 0;
	
	public AmfClient(String endPoint){
		this.endPoint = endPoint;
		this.request  = new AmfMessage();
		this.response = new AmfMessage();
	}

	public void resetRequest(){
		this.request  = new AmfMessage();
		this.response = new AmfMessage();
	}
	public void addHeader(String name, boolean required, Object data){
		this.request.addHeader(new AmfHeader(name, data, required));
	}
	
	public void setCredentials(String username, String password){
		Hashtable ht = new Hashtable();
		ht.put("userid", username);
		ht.put("password", password);
		Vector keys = new Vector();
		keys.addElement("userid");
		keys.addElement("password");
		ht.put("__keys__",keys);
		this.addHeader("Credentials", false, ht);
	}
	
	public void setEncoding(int encoding){
		this.encoding = encoding;
	}
	
	public void sendRequest(String servicePath, Object[] data) throws IOException{
		if(this.encoding==AmfConstants.AMF3){
			//TODO: procesar amf3 encoding
		}
		long time_start, time_end;
		time_start = System.currentTimeMillis();
		this.request.addBody(new AmfBody(servicePath, "/1", data));
//		OutputConnection connection = (OutputConnection)Connector.open("file:///SDCard/databases/javaRequest.txt", Connector.WRITE );
//		this.outputStream=connection.openDataOutputStream();
//		this.request.serialize(this.outputStream);
//		this.outputStream.close();
//		connection.close();
		HttpConnection httpConnection = (HttpConnection) Connector.open(this.endPoint);
        httpConnection.setRequestMethod(HttpConnection.POST);
        httpConnection.setRequestProperty("Content-type", AmfConstants.MIMETYPE);
        this.outputStream = httpConnection.openDataOutputStream();
		this.request.serialize(this.outputStream);
		this.outputStream.close();
		int responseCode = httpConnection.getResponseCode(); 
		if(responseCode!=HttpConnection.HTTP_OK)
			throw new IOException("HTTP-ERROR: " + responseCode);
        this.inputStream = httpConnection.openDataInputStream();
        
//		connection = (OutputConnection)Connector.open("file:///SDCard/databases/javaResponse.txt", Connector.WRITE );
//		byte[] response = new byte[Integer.valueOf(httpConnection.getLength()+"").intValue()];
//		this.inputStream.readFully(response);
//		this.outputStream=connection.openDataOutputStream();
//		outputStream.write(response);
//		this.outputStream.close();
//        this.response.deserialize(new DataInputStream(new ByteArrayInputStream(response)));
        this.response.deserialize(this.inputStream);
		time_end = System.currentTimeMillis();
		this.elapsedTime = time_end - time_start;
	}
	
	public AmfMessage getResponse(){
		return this.response;
	}
	
	public long getElapsedTime(){
		return this.elapsedTime;
	}
}